﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace ConsoleApp_e1e2
{
    public class Order
    {
        public int id
        { set; get; }//订单号
        public string commodity
        { set; get; }//商品名称
        public string client
        { set; get; }//客户
        public int money
        { set; get; }//金额
        public Order() { }
        public Order(int id, string commodity, string client, int money)
        {
            this.id = id;
            this.commodity = commodity;
            this.client = client;
            this.money = money;
        }
        public override bool Equals(object obj)
        {
            if (obj is Order)
            {
                Order m = (Order)obj;
                if (m.id == id && m.commodity == commodity && m.client == client && m.money == money)
                    return true;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(id, commodity, client, money);
        }

        public override string ToString()
        {
            return id + " " + commodity + " " + client + " " + money;
        }
    }
    [Serializable]
    public class OrderDetails : Order
    {
        public double date { set; get; }
        public double time { set; get; }
        public int num { set; get; }//下单日期，下单时间，商品数量
        public OrderDetails() : base() { }
        public OrderDetails(int id, string commodity, string client, int money, double date, double time, int num) : base(id, commodity, client, money)
        {
            this.date = date;
            this.time = time;
            this.num = num;
        }
        public override bool Equals(object obj)
        {
            if (obj is OrderDetails)
            {
                OrderDetails m = (OrderDetails)obj;
                if (m.date == date && m.time == time && m.num == num)
                    return base.Equals(obj);
            }
            return false;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(base.GetHashCode(), id, commodity, client, money, date, time, num);
        }

        public override string ToString()
        {
            return base.ToString() + " " + date + " " + time + " " + num;
        }
    }
    public class OrderService
    {
        public List<OrderDetails> list = new List<OrderDetails>();
        public void Export(XmlSerializer ser, string fileName, object obj)
        {
            FileStream fs = new FileStream(fileName, FileMode.Create);
            ser.Serialize(fs, obj);
            fs.Close();
        }
        public void Import(XmlSerializer ser, string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open);
            List<OrderDetails> orderxml = (List<OrderDetails>)ser.Deserialize(fs);
            foreach (OrderDetails n in orderxml)
            {
                Console.WriteLine(n.ToString());
            }
        }
        public void add(OrderDetails order)
        {
            foreach (var a in list)
            {
                if (a.Equals(order) == true)
                {
                    Console.WriteLine("已有重复的订单，不能继续添加");
                    return;
                }
            }
            list.Add(order);
        }
        public bool search1(int id)
        {
            var order = from m in list where m.id == id orderby m.money select m;
            if (order.Count() == 0)
                return false;
            else
            {
                foreach (OrderDetails n in order)
                {
                    Console.WriteLine(n.ToString());
                }
                return true;
            }
        }
        public bool search2(string commodity)
        {
            var order = from m in list where m.commodity == commodity orderby m.money select m;
            if (order.Count() == 0) return false;
            else
            {
                foreach (OrderDetails n in order)
                {
                    Console.WriteLine(n.ToString());
                }
                return true;
            }
        }
        public bool search3(string client)
        {
            var order = from m in list where m.client == client orderby m.money select m;
            if (order.Count() == 0) return false;
            else
            {
                foreach (OrderDetails n in order)
                {

                    Console.WriteLine(n.ToString());
                }
                return true;
            }
        }
        public bool search4(int money)
        {
            var order = from m in list where m.money == money orderby m.money select m;
            if (order.Count() == 0) return false;
            else
            {
                foreach (OrderDetails n in order)
                {
                    Console.WriteLine(n.ToString());
                }
                return true;
            }
        }
        public bool remove(OrderDetails order)
        {
            bool flag = false;
            OrderDetails od = null;
            foreach (OrderDetails n in list)
            {
                if (n.id == order.id)
                {
                    od = n;
                    flag = true;
                }
            }
            if (flag)
                list.Remove(od);
            return flag;
        }
        public bool modify(OrderDetails order)
        {
            foreach (OrderDetails n in list)
            {
                if (n.id == order.id)
                {
                    n.commodity = order.commodity;
                    n.client = order.client;
                    n.money = order.money;
                    n.date = order.date;
                    n.time = order.time;
                    n.num = order.num;
                    return true;
                }
            }
            return false;
        }
        public void view()
        {
            foreach (OrderDetails n in list)
            {
                Console.WriteLine(n.ToString());
            }
        }
        public void sort1()
        {
            var m = list.OrderBy(s => s.id);
            list = m.ToList();
        }
        public void sort2()
        {
            var m = list.OrderByDescending(s => s.id);
            list = m.ToList();
        }
        public void sort3()
        {
            var m = list.OrderBy(s => s.money);
            list = m.ToList();
        }
        public void sort4()
        {
            var m = list.OrderByDescending(s => s.money);
            list = m.ToList();
        }
        public void sort5()
        {
            var m = list.OrderByDescending(s => s.date);
            list = m.ToList();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            OrderService service = new OrderService();
            OrderDetails order1 = new OrderDetails(1001, "book", "teacher", 65, 3.28, 11.30, 25);
            OrderDetails order2 = new OrderDetails(1002, "pen", "student", 10, 1.28, 18.30, 1);
            OrderDetails order3 = new OrderDetails(1003, "phone", "Jack", 3900, 3.2, 15.23, 1);
            OrderDetails order4 = new OrderDetails(1004, "tea", "Jane", 245, 1.11, 11.39, 2);
            OrderDetails order5 = new OrderDetails(1005, "book", "student", 70, 3.29, 8.22, 1);
            OrderDetails order6 = new OrderDetails(1001, "book", "teacher", 60, 3.29, 8.22, 30);
            service.add(order1);
            service.add(order2);
            service.add(order1);
            service.add(order3);
            service.add(order4);
            service.add(order5);
            service.view();
            Console.WriteLine();
            service.search1(1003);
            Console.WriteLine();
            service.search2("book");
            Console.WriteLine();
            service.search3("student");
            Console.WriteLine();
            service.search4(245);
            Console.WriteLine();
            service.remove(order4);
            service.modify(order6);
            service.sort1();
            Console.WriteLine();
            service.view();
            service.sort2();
            Console.WriteLine();
            service.view();
            service.sort3();
            Console.WriteLine();
            service.view();
            service.sort4();
            Console.WriteLine();
            service.view();
            XmlSerializer xmlser = new XmlSerializer(typeof(List<OrderDetails>));
            string xmlFileName = "s.xml";
            service.Export(xmlser, xmlFileName, service.list);
            Console.WriteLine();
            service.Import(xmlser, xmlFileName);
        }
    }
}
